package com.bz.sunmipay.payment;

import android.content.Context;

import com.bz.sunmipay.utils.AppUtils;
import com.wirecard.ecom.card.model.CardPayment;
import com.wirecard.ecom.model.TransactionType;

import java.math.BigDecimal;
import java.util.Set;
import java.util.UUID;

import de.wirecard.paymentsdk.WirecardClient;
import de.wirecard.paymentsdk.WirecardClientBuilder;
import de.wirecard.paymentsdk.WirecardEnvironment;
import de.wirecard.paymentsdk.WirecardException;
import de.wirecard.paymentsdk.WirecardResponseListener;
import de.wirecard.paymentsdk.WirecardTransactionType;
import de.wirecard.paymentsdk.api.models.CardBundle;
import de.wirecard.paymentsdk.helpers.card.CardTypeUtil;
import de.wirecard.paymentsdk.models.CardToken;
import de.wirecard.paymentsdk.models.CustomerData;
import de.wirecard.paymentsdk.models.PaymentPageStyle;
import de.wirecard.paymentsdk.models.WirecardAlipayPayment;
import de.wirecard.paymentsdk.models.WirecardExtendedCardPayment;

/**
 * 支付
 * <p>
 * Created by Administrator on 2019/11/14.
 */
public class PaymentHelp {
    public final static String URL_EE_TEST = "https://api-test.wirecard.com";
    private static final String TAG = "WireCard";
    private Context mContext;
    private static PaymentHelp INSTANCE;
    private WirecardClient wirecardClient;

    private WirecardExtendedCardPayment wirecardExtendedCardPayment;
    private WirecardAlipayPayment wirecardAlipayPayment;

    private PaymentHelp(Context context) {
        mContext = context;
        wirecardExtendedCardPayment = new WirecardExtendedCardPayment(null, null, null, null,
                null, new BigDecimal(0), null);

        wirecardAlipayPayment = new WirecardAlipayPayment(
                null, null, null, null, WirecardTransactionType.AUTHORIZATION,
                new BigDecimal(0), null);
    }

    public static synchronized PaymentHelp getInstance(Context context) {
        if (INSTANCE == null) {
            INSTANCE = new PaymentHelp(context.getApplicationContext());
        }
        return INSTANCE;
    }

    public void initWireCard() {

        String environment = WirecardEnvironment.TEST.getValue();
        try {
            wirecardClient = WirecardClientBuilder.newInstance(mContext, environment)
                    .setRequestTimeout(60)
                    .build();
        } catch (WirecardException exception) {
            //device is rooted
        }
    }

    //transactionType交易类型 储蓄卡 CNY -> iso 人民币
    public void makeSimplePayment(String cardNum, String securityCode, String money, WirecardResponseListener listener) {
        String timestamp = AppUtils.generateTimestamp();
        String merchantID = "33f6d473-3036-4ca5-acb5-8c64dac862d1";
        String secretKey = "9e0130f6-2e1e-4185-b0d5-dc69079c75cc";
        String requestID = UUID.randomUUID().toString();
        WirecardTransactionType transactionType = WirecardTransactionType.PURCHASE;
        BigDecimal amount = new BigDecimal(money);
        String currency = "EUR";

        String signature = AppUtils.generateSignatureV2(timestamp, merchantID, requestID,
                transactionType.getValue(), amount, currency, secretKey);

        wirecardExtendedCardPayment.setRequestTimeStamp(timestamp);
        wirecardExtendedCardPayment.setMerchantAccountID(merchantID);
        wirecardExtendedCardPayment.setRequestID(requestID);
        wirecardExtendedCardPayment.setTransactionType(transactionType);
        wirecardExtendedCardPayment.setAmount(amount);
        wirecardExtendedCardPayment.setCurrency(currency);
        wirecardExtendedCardPayment.setSignature(signature);

        //append last name
        CustomerData accountHolder = new CustomerData();
//        accountHolder.setLastName("Doe");
        wirecardExtendedCardPayment.setAccountHolder(accountHolder);

        CardToken cardToken = new CardToken();
        cardToken.setMaskedAccountNumber("6225683739000590376");
        cardToken.setTokenId("4193258203791111");
//        wirecardExtendedCardPayment.setCardToken(cardToken);

        CardBundle cardBundle = new CardBundle();
        cardBundle.setCardSecurityCode(securityCode);
        cardBundle.setLastName("");
        cardBundle.setFirstName("");
        cardBundle.setCardBrand(CardTypeUtil.detectCard(cardNum, null).getName());
        //4271149787014678  6225768725627424
        cardBundle.setCardNumber(cardNum);
        cardBundle.setCardDate("12/2020");
        wirecardExtendedCardPayment.setCardBundle(cardBundle);
        wirecardClient.makePayment(wirecardExtendedCardPayment, new PaymentPageStyle(), listener);
    }

    public CardPayment getCardTokenPayment() {
        String timestamp = AppUtils.generateTimestamp();
        String merchantID = "33f6d473-3036-4ca5-acb5-8c64dac862d1";
        String secretKey = "9e0130f6-2e1e-4185-b0d5-dc69079c75cc";
        String requestID = UUID.randomUUID().toString();
        TransactionType transactionType = TransactionType.PURCHASE;
        BigDecimal amount = new BigDecimal(1);
        String currency = "EUR";
        String signature = AppUtils.generateSignatureV2(timestamp, merchantID, requestID, transactionType.getValue(), amount, currency, secretKey);

        CardPayment cardPayment = new CardPayment.Builder()
                .setSignature(signature)
                .setMerchantAccountId(merchantID)
                .setRequestId(requestID)
                .setAmount(amount)
                .setTransactionType(transactionType)
                .setCurrency(currency)
                .build();
        cardPayment.setRequireManualCardBrandSelection(true);
        cardPayment.setAnimatedCardPayment(false);

        return cardPayment;
    }
}
